package com.developer.platform.application.query.assembler;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.developer.platform.application.query.dto.DictDataCutDto;
import com.developer.platform.application.query.dto.DictDataDto;
import com.developer.platform.application.query.dto.DictTypeListDto;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.domain.DictData;
import com.developer.platform.domain.DictType;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-10-28
 */
@Component
public class DictAssembler extends BaseAssembler{


    public Map<String, List<DictDataCutDto>> toDictDataCutDto(List<DictData> list){
        return list.stream()
                .map(dict-> new DictDataCutDto(dict.getType(),dict.getLabel(),dict.getValue(),
                        dict.getListClass()))
                .collect(Collectors.groupingBy(DictDataCutDto::getType));
    }


    public ResponseModel<DictTypeListDto> toResponseModel(IPage<DictType> page){
        List<DictTypeListDto> collect = page.getRecords().stream().map(dictType -> {
            DictTypeListDto dictTypeListDto = new DictTypeListDto();
            BeanUtil.copyProperties(dictType, dictTypeListDto);
            return dictTypeListDto;
        }).collect(Collectors.toList());
        this.userName(collect);
        return new ResponseModel<>(collect, (int)page.getTotal());
    }


    public List<DictDataDto> toDictDataDto(List<DictData> list){
        List<DictDataDto> collect = list.stream().map(dictData -> {
            DictDataDto dictDataDto = new DictDataDto();
            BeanUtil.copyProperties(dictData, dictDataDto);
            return dictDataDto;
        }).collect(Collectors.toList());
        this.userName(collect);
        return collect;
    }

}
